package it.fuesi.synoutils.activities;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

import it.fuesi.synoutils.R;
import it.fuesi.synoutils.activities.base.ADetailActivity;
import it.fuesi.synoutils.helpers.FormatHelper;
import it.fuesi.synoutils.model.download.DownloadStationClientParameters;
import it.fuesi.synoutils.model.download.DownloadTask;
import it.fuesi.synoutils.services.tasks.download.DeleteDownloadTask;
import it.fuesi.synoutils.services.tasks.download.PauseDownloadTask;
import it.fuesi.synoutils.services.tasks.download.ResumeDownloadTask;


public class DownloadTaskDetailActivity extends ADetailActivity<DownloadTask> {

    private TextView mTitle;
    private TextView mSize;
    private TextView mDownloaded;
    private TextView mStatus;

    public static final int REQUEST_CODE = 102;


    @Override
    public int getLayoutId() {
        return R.layout.activity_download_task_detail;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mTitle = (TextView)findViewById(R.id.task_title);
        mSize = (TextView)findViewById(R.id.task_size);
        mDownloaded = (TextView)findViewById(R.id.task_downloaded);
        mStatus = (TextView)findViewById(R.id.task_status);

        mTitle.setText(getItem().getTitle());
        mStatus.setText(getItem().getStatus());
        mSize.setText(FormatHelper.formatFileSize(getItem().getSize()));
        mDownloaded.setText(FormatHelper.formatFileSize(getItem().getAdditional().getTransfer().getSize_downloaded()));
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.download_task_detail, menu);

        final MenuItem itemPause = menu.findItem(R.id.action_pause);
        final MenuItem itemResume = menu.findItem(R.id.action_resume);

        switch(getItem().getTaskStatus()){
            case paused:{
                itemPause.setVisible(false);
                itemResume.setVisible(true);
            }break;
            case finished:
            case finishing:
            case hash_checking:
            case extracting:
            {
                itemPause.setVisible(false);
                itemResume.setVisible(false);
            }break;
            default:{
                itemPause.setVisible(true);
                itemResume.setVisible(false);
            }

        }


        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_delete) {

            deleteTask();

            return true;
        }else if(id == R.id.action_pause){
            pauseTask();
            return true;
        }else if(id == R.id.action_resume){
            resumeTask();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void deleteTask(){
        DownloadStationClientParameters params = new DownloadStationClientParameters();
        params.setTaskId(getItem().getId());
        new DeleteDownloadTask(this).execute(params);
    }

    private void pauseTask(){
        final DownloadStationClientParameters params = new DownloadStationClientParameters();
        params.setTaskId(getItem().getId());
        new PauseDownloadTask(this).execute(params);
    }

    private void resumeTask(){
        final DownloadStationClientParameters params = new DownloadStationClientParameters();
        params.setTaskId(getItem().getId());
        new ResumeDownloadTask(this).execute(params);
    }
}
